home *** CD-ROM | disk | FTP | other *** search
- //
- // player.js
- // firefox
- //
- // Created by Zak on 2008-06-18.
- // Contributor Brian King (http://briks.si)
- // Copyright 2008-2009 Ant.com. All rights reserved.
- //
-
- /**
- * AntPlayer: Handle all actions inside the player.xul window
- */
- var AntPlayer =
- {
- movieFileMatch: /^([\w-]*)\.?(.*)\.(?:flv|3gpp|mp4|asf|aac)$/i,
- doc: null,
- listBox: null,
- currentVideo: '',
- flashVars: '&javascriptid=antFlvPlayerSwf&enablejs=true&backcolor=0xFFFFFF&autoscroll=true&searchbar=false&debug=true',
- cleanedSearchBox: false,
-
-
- /**
- * Initialize the list
- * @param doc The document window
- */
- init: function (doc)
- {
- var self = AntPlayer;
- var playOnLoad = false;
-
- self.doc = doc;
- self.listBox = AntLib.ob('antFlvPlayerRichListBox', doc);
- self.antSplash();
- self.fillVideos();
-
- window.addEventListener("load", self.autoPlay(playOnLoad), false);
-
- //unload because the movie keep running when leaving the player with a bookmark link
- window.addEventListener("unload", function (){ self.antSplash(); }, false);
- window.addEventListener("resize", function (){ self.playVideo(self.currentVideo, true); }, false);
- },
-
- extractFlvOrigin: function (filename)
- {
- return filename.replace(/\..*/, "");
- },
-
- extractFlvName: function (filename)
- {
- return filename.replace(/\.[^.]{2,4}$/i, "").replace(/^\w+\./i, "");
- },
-
- fileExist: function (path)
- {
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
- file.initWithPath(path);
- return file.exists();
- },
-
- /**
- * Auto play a video when the player loads using preference or trying to guess the last download
- */
- autoPlay: function (playOnLoad)
- {
- var self = AntPlayer;
-
- return function ()
- {
- var splitter = AntLib.ob("antFlvPlayerSplitter");
- var embed = AntLib.ob("antFlvPlayerEmbed");
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
-
- splitter.addEventListener("mouseup", function (){ self.playVideo(self.currentVideo, true); }, true);
-
- var flvToPlay = false;
- if (AntPrefs.flvToPlay) {
- file.initWithPath(AntPrefs.flvToPlay);
- flvToPlay = true;
- }
-
- if (!flvToPlay || !file.exists())
- {
- var index = self.listBox.getIndexOfFirstVisibleRow();
- if (isNaN(index) || index < 0 || index >= self.listBox.childNodes.length)
- return;
-
- var nodes = self.listBox.childNodes[index].childNodes;
- self.playVideo(nodes[1].getAttribute('label'), playOnLoad);
- }
- else
- {
- self.playVideo(AntPrefs.flvToPlay, playOnLoad);
- }
-
- }
- },
-
- /**
- * getItemByUrl
- */
- colorItemByUrl: function (url)
- {
- var self = AntPlayer;
-
- for (var i = 0; i < self.listBox.childNodes.length; i++)
- {
- var item = self.listBox.childNodes[i];
- var className = "antFlvPlayerRichListItem";
-
- if (!item)
- return;
- if (url == item.path)
- {
- className = item.getAttribute('class') + " antFlvPlayerRichListItemActive";
- item.setAttribute('class', className);
- }
- else
- {
- if ((i % 2) == 0)
- className += " antFlvPlayerRichListItemGrey";
- item.setAttribute('class', className);
- }
- }
- return null;
- },
-
-
- /**
- * Handle search using terms typed in the antToolBarSearchBox
- * @param event The event that triggered the call to self callback
- * @param type The type of search (web,image,video,news,blog)
- */
- doSearch: function (type)
- {
- var self = AntPlayer;
- var searchTermsBox = AntLib.ob("antFlvPlayerSearchBox");
- var searchTerms = AntLib.trim(searchTermsBox.value);
-
- if (searchTerms.length == 0)
- {
- AntLib.openURL('http://www.ant.com/');
- }
- else
- {
- searchTerms = AntLib.uriEncode(searchTerms);
- AntLib.openURL('http://www.ant.com/'+type+'/'+searchTerms);
- }
- },
-
- /**
- * Clear the searchbox the first time user enter it
- * @param isFocus true if the callback is onFocus otherwise false
- */
- onSearchBoxOnFocus: function (isFocus)
- {
- var self = AntPlayer;
- var searchBox = AntLib.ob('antFlvPlayerSearchBox');
-
- if (!self.cleanedSearchBox && isFocus)
- {
- searchBox.value = '';
- self.cleanedSearchBox = true;
- }
-
- if (!isFocus && !searchBox.value)
- {
- searchBox.value = AntLang.getString('AntBar.SearchWithAnt');
- self.cleanedSearchBox = false;
- }
- },
-
- /**
- * Handle keystrokes entered in the searchBox
- * @param event The event that triggered the call to self function
- */
- onSearchBoxKeyPress: function (event)
- {
- var self = AntPlayer;
- if (event.keyCode == event.DOM_VK_RETURN && AntLib.ob('antFlvPlayerSearchBox').value != '')
- self.doSearch('web');
- },
-
- /**
- * Display the Splash Screen
- */
- antSplash: function ()
- {
- var self = AntPlayer;
- var embed = AntLib.ob("antFlvPlayerEmbed");
- var e = AntLib.uriEncode;
-
- embed.setAttribute("flashvars", "file=" + e("chrome://antbar/content/player/skin/img/ant.jpg") + "&image=chrome://antbar/content/player/skin/img/ant.jpg&shownavigation=false&javascriptid=antFlvPlayerSwf&enablejs=true&frontcolor=0xFFFFFF&lightcolor=0xFFFFFF&screencolor=0xFFFFFF&searchbar=false&showicons=false&shownavigation=false");
- AntLib.ob("antFlvPlayerContentBox").removeChild(AntLib.ob("antFlvPlayerContentBox").firstChild);
- AntLib.ob("antFlvPlayerContentBox").insertBefore(embed, AntLib.ob("antFlvPlayerSplitter"));
- self.fillTitlebar("Ant.com", "Player");
- },
-
- fillTitlebar: function (aOrigin, aName)
- {
- var self = AntPlayer;
- // Fix to remove innerHTML insertion by Wladimir Palant, integrated by Brian King (http://briks.si)
- var str = AntLang.getFormatString("AntPlayer.TitleBar", aOrigin, aName);
- var element = AntLib.ob("antFlvPlayerTitleBar");
- if (/(.*?)<html:strong>(.*?)<\/html:strong>(.*)/.test(str))
- {
- var strong = document.createElementNS("http://www.w3.org/1999/xhtml", "strong");
- strong.textContent = RegExp.$2;
-
- element.textContent = ""; // reset
- element.appendChild(document.createTextNode(RegExp.$1));
- element.appendChild(strong);
- element.appendChild(document.createTextNode(RegExp.$3));
- }
- else
- element.textContent = str;
- // end fix
- },
-
- formatDisplayStrings: function (flvlink)
- {
- var self = AntPlayer;
- var humanizedName = flvlink.name.replace(/_/g, ' ');
- var infosString = flvlink.origin;
-
- infosString = AntLib.upFirstLetter(infosString);
- humanizedName = AntLib.upFirstLetter(humanizedName);
- if (flvlink.filesize)
- infosString += " - " + flvlink.filesize;
- if (flvlink.date)
- {
- var date = new Date(flvlink.date);
- var day = date.getDate();
- var month = date.getMonth();
- var year = date.getFullYear();
- var hour = date.getHours();
- var minute = date.getMinutes();
- var formatedHour = day+"/"+month+"/"+year+" "+hour+":"+minute;
-
- infosString += " - " + formatedHour;
- }
-
- return [humanizedName, infosString];
- },
-
- /**
- * Add a ListItem to the ListBox
- * @param origin Origin of the flv
- * @param url Url of the flv
- * @param name Guessed name of the flv
- * @param id A unique ID for the listItem
- */
- appendItem: function (flvlink, id)
- {
- var self = AntPlayer;
- var className = "antFlvPlayerRichListItem";
- var richListItem = self.doc.createElement('richlistitem');
- var nameLabel = self.doc.createElement('label');
- var originLabel = self.doc.createElement('label');
- var strings = self.formatDisplayStrings(flvlink);
-
- if ((id % 2) == 0)
- className += " antFlvPlayerRichListItemGrey";
- richListItem.setAttribute('class', className);
- richListItem.setAttribute('ondblclick', 'AntPlayer.onListItemDblClick(event);');
- richListItem.setAttribute('context', "antFlvPlayerPopupMenu");
- richListItem.setAttribute('orient', "vertical");
- richListItem.path = flvlink.url;
-
- nameLabel.setAttribute('value', strings[0]);
- nameLabel.setAttribute('class', 'antFlvPlayerRichListName');
- nameLabel.setAttribute('crop', 'end');
-
- originLabel.setAttribute('value', strings[1]);
- originLabel.setAttribute('class', 'antFlvPlayerRichListInfos');
- originLabel.setAttribute('crop', 'end');
-
- richListItem.appendChild(nameLabel);
- richListItem.appendChild(originLabel);
-
- self.listBox.appendChild(richListItem);
- },
-
-
- /**
- * Redraw the iframe with needed parameters to play the specified file
- * @param path The full file path to play (Format: /tmp/origin.name.flv)
- */
- playVideo: function (path, start)
- {
- if (path == "") return;
- if (typeof(start) == 'undefined') start = true;
-
- var self = AntPlayer;
- var embed = AntLib.ob("antFlvPlayerEmbed");
- var e = AntLib.uriEncode;
-
- var file_path = e('file://' + path);
- var query = "&file=" + file_path + self.flashVars + "&autostart=" + (start ? "true" : "false");
-
- AntLib.toLog("Playing: " + file_path);
-
- embed.setAttribute("flashvars", query);
- AntLib.ob("antFlvPlayerContentBox").removeChild(AntLib.ob("antFlvPlayerContentBox").firstChild);
- AntLib.ob("antFlvPlayerContentBox").insertBefore(embed, AntLib.ob("antFlvPlayerSplitter"));
-
- try
- {
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
- file.initWithPath(path);
-
- var origin = self.extractFlvOrigin(file.leafName);
- var name = self.extractFlvName(file.leafName);
-
- name = name.replace(/_/g, " ");
-
- self.fillTitlebar(origin, name);
- }
- catch (e)
- {}
-
- self.currentVideo = path;
- self.colorItemByUrl(path);
- },
-
- /**
- * Oncommand Logo Click
- */
- onLogoClick: function ()
- {
- AntLib.openURL('http://www.ant.com/');
- },
-
- /**
- * Callback: fired when the user double click an item in the list
- * @param event The event that triggered the call to this method
- */
- onListItemDblClick: function (event)
- {
- var self = AntPlayer;
- var node = event.target;
-
- while (node.nodeName != "richlistitem")
- node = node.parentNode;
- self.playVideo(node.path);
- },
-
- /**
- * Callback: fired when the user click on "Play" in the context menu
- */
- onPopupPlay: function ()
- {
- var self = AntPlayer;
- var node = document.popupNode;
-
- while (node.nodeName != "richlistitem")
- node = node.parentNode;
- self.playVideo(node.path);
- },
-
- /**
- * Callback: fired when the user click on "Rename" in the context menu
- */
- onPopupRename: function ()
- {
- var self = AntPlayer;
- var node = document.popupNode;
-
- while (node.nodeName != "richlistitem")
- node = node.parentNode;
- var path = node.path;
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
-
- file.initWithPath(path);
-
- var origin = self.extractFlvOrigin(file.leafName);
- var name = self.extractFlvName(file.leafName);
- if (self.isCurrentlyPlaying(file.path))
- return ;
-
- name = name.replace(/_/g,' ');
- var newName = prompt(AntLang.getFormatString("AntPlayer.PromptRename", name), name);
-
- if (!newName)
- return ;
-
- if (!newName.match(/^[\w -]+$/i))
- {
- alert(AntLang.getString("AntPlayer.InvalidFileName"));
- return
- }
- newName = AntLib.sanitize(newName);
- if (file.exists())
- {
- try
- {
- file.moveTo(file.parent, origin + "." + newName + ".flv");
- }
- catch (e)
- {
- alert(AntLang.getString("AntPlayer.ErrorRenaming"));
- return ;
- }
- }
- self.fillVideos();
- },
-
-
- /**
- * Check if the given video path is currently playing
- * @param path The path to check
- */
- isCurrentlyPlaying: function (path)
- {
- if (self.currentVideo == path)
- {
- alert(AntLang.getString("AntPlayer.ErrorCurrentlyPlaying"));
- return true;
- }
-
- return false;
- },
-
- /**
- * Callback: fired when the user click on "Delete" in the context menu
- */
- onPopupDelete: function ()
- {
- var self = AntPlayer;
- var node = document.popupNode;
-
- while (node.nodeName != "richlistitem")
- node = node.parentNode;
- var path = node.path;
- var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
- var name;
-
- file.initWithPath(path);
- name = self.extractFlvName(file.leafName);
-
- if (confirm(AntLang.getFormatString("AntPlayer.ConfirmDelete", name)))
- {
- if (self.currentVideo == path)
- self.antSplash();
- try {
- file.remove(false);
- }
- catch (e) {
- alert(AntLang.getString("AntPlayer.ErrorDeleting"));
- AntLib.toLog("ERROR: AntPlayer.onPopupDelete: " + e);
- return ;
- }
- self.fillVideos();
- }
- },
-
- /**
- * Remove all listItems from the ListBox
- */
- removeAll: function ()
- {
- var self = AntPlayer;
-
- while (self.listBox.lastChild)
- self.listBox.removeChild(self.listBox.lastChild);
- },
-
- /**
- * Fetch videos from a directory and return an Array of AntFlvLink
- */
- fetchVideos: function ()
- {
- var self = AntPlayer;
- var pref = AntPrefs.getAntBranch();
- if (!pref)
- {
- alert(AntLang.getString("AntDownloadManager.InvalidDestinationFolder"));
- return null;
- }
- var dir = pref.getCharPref("flvdir");
- var videoDirectory = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
- var entries;
- var arr = new AntArray();
-
- videoDirectory.initWithPath(dir);
- if (!videoDirectory.isDirectory())
- return new AntArray();
-
- entries = videoDirectory.directoryEntries;
- while (entries.hasMoreElements())
- {
- var entry = entries.getNext();
- var m;
-
- AntLib.QI(entry, AntLib.CI("nsIFile"));
- if (m = entry.leafName.match(self.movieFileMatch))
- {
- var origin = m[1];
- var name = m[2];
-
- var url = entry.path;
- var lastModifiedTime = entry.lastModifiedTime;
- var filesize = AntLib.convertByteUnits(entry.fileSize);
-
- arr.push(new AntFlvLink({origin: origin, url:url, name:name, date:lastModifiedTime, filesize:filesize[0] + " " + filesize[1]}));
- }
- }
-
- return arr;
- },
-
- /**
- * Fill the list with the videos
- */
- fillVideos: function ()
- {
- var self = AntPlayer;
- if (self.listBox.childNodes.length > 0)
- self.removeAll();
- var listVideos = self.fetchVideos();
-
- for (var i = 0; i < listVideos.length; i++)
- self.appendItem(listVideos[i], i);
- },
- }
-